package com.zys.controller;

import com.zys.domain.Result;
import com.zys.domain.Teacher;
import com.zys.service.TeacherService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/teacher")
public class TeacherController {
    private final TeacherService teacherService;

    private final ApplicationContext applicationContext;

    @PostMapping("/save")
    public Result save(@RequestBody Teacher teacher){
        teacherService.save(teacher);
        applicationContext.publishEvent(new Teacher(1,0));
        return Result.success();
    }

    @GetMapping("/get/{id}")
    @Cacheable(cacheNames = "teacher",key = "#id")
    public Result<Teacher> get(@PathVariable Integer id){
        return Result.success(teacherService.getById(id));
    }
}
